﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IFTRUE.FMS.Web.Ashx
{
    /// <summary>
    /// UpdatePwd 的摘要说明
    /// </summary>
    public class UpdatePwd : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            //安全验证
            string strMsg = "";
            if (!AshxCheck.Check(context, ref strMsg))
            {
                context.Response.Write(SysMsg.GetCommonMsg(strMsg));
                return;
            }
            
            //输入内容验证
            if (string.IsNullOrEmpty(context.Request["oldPwd"]))
            {
                context.Response.Write(SysMsg.GetCommonMsg("请输入旧密码"));
                return;
            }
            if (string.IsNullOrEmpty(context.Request["newPwd"]))
            {
                context.Response.Write(SysMsg.GetCommonMsg("请输入新密码"));
                return;
            }
            if (string.IsNullOrEmpty(context.Request["rePwd"]))
            {
                context.Response.Write(SysMsg.GetCommonMsg("请输入确认新密码"));
                return;
            }
            BLL.UserInfo userInfoBLL = new BLL.UserInfo();
            Model.UserInfo userInfoModel = userInfoBLL.Get(MySession.CurrentUser.Account, context.Request["oldPwd"].Trim());
            if (userInfoModel == null)
            {
                context.Response.Write(SysMsg.GetCommonMsg("旧密码不正确"));
                return;
            }
            userInfoModel.Password = Helper.Encrypt.MD5(context.Request["newPwd"].Trim());
            bool bl = new BLL.UserInfo().Update(userInfoModel, "Password", typeof(string));
            new BLL.Log().Add(bl ? 2 : 3, "修改密码", string.Format("用户修改密码（{0}）", userInfoModel.Account));
            if (bl)
            {
                context.Response.Write(SysMsg.GetSuccessMsg("UpdatePwd","forward"));
            }
            else
            {
                context.Response.Write(SysMsg.Fail);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}